package RabbitMQ.WorkQueue;
import RabbitMQ.Constants.Constants;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class ProducerDemo {
    public static void main(String[] args) throws IOException, TimeoutException {
//        1.建立连接
        ConnectionFactory connectionFactory=new ConnectionFactory();
        connectionFactory.setHost(Constants.HOST);
        connectionFactory.setPort(Constants.PORT);
        connectionFactory.setUsername(Constants.USER_NAME);
        connectionFactory.setPassword(Constants.PASSWORD);
        connectionFactory.setVirtualHost(Constants.VIRTUAL_HOST);
        Connection connection=connectionFactory.newConnection();
//       2.建立信道
        Channel channel=connection.createChannel();
//        3.声明交换机,使用内置的交换机
//        4.队列名称.可持久化.是否独占.是否自动删除.参数.
        channel.queueDeclare(Constants.WORK_QUEUE,true,false,false,null);
//        发送消息
        for(int i=0;i<10;i++){
            String str="hello,Work_Queue "+i;
            channel.basicPublish("", Constants.WORK_QUEUE, null, str.getBytes());
//            交换机名称,使用内置交换机,routingKey和队列名保持一致   配置的属性   参数
        }
        System.out.println("消息发送成功");
//        释放连接
        channel.close();
        connection.close();
    }
}
